<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBClientAction_password extends KBClientAction_common
{

	function &execute($controller, $manager) {
		
		//require_once $dir . 'KBClientView_password.php';
		//$view = &new KBClientView_password($controller);
		$view = &$controller->getView();
		
		if(isset($this->rp->submit)) {
			
			$pass = new PasswordUtil;
			$pass->table = $manager->tbl->user;	
			
			$v =& new Validator($this->rp->vars, false);
			$v->regex('email_msg', 'email', 'email');
		
			$user_id = $pass->isEmailExists($this->rp->vars['email']);
			if(!$user_id) { $v->setError('email_not_exists_msg', '1'); } 
			
			if($v->getErrors()) {
				$this->rp->stripVars(true);
				$view->setErrors($v->getErrors());
				$view->setFormData($this->rp->vars);
			
			} else {
				$user = $manager->getUserInfo($user_id);
				$password = $pass->generatePassword();
				$sent = $manager->sendGeneratedPassword($password, $user, $view);
				
				if($sent) {
					$this->rp->stripVars();
					$pass->setPassword($user_id, MD5($password));
					$controller->go('success_go', false, false, 'password_sent');
				
				} else {
					$this->rp->stripVars(true);
					$view->setFormData($this->rp->vars);
					$view->msg_id = 'password_not_sent';
				}
			}
		}
		
		return $view;
	}
	
}
?>